function formatCurrentDate(days?: number): string {
  const now = new Date();
  if (days) {
    now.setDate(days || 0);
  }
  // 获取年、月、日、时、分、秒
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从 0 开始，所以要加 1
  const day = String(now.getDate()).padStart(2, '0');
  const hours = String(now.getHours()).padStart(2, '0');
  const minutes = String(now.getMinutes()).padStart(2, '0');
  const seconds = String(now.getSeconds()).padStart(2, '0');

  // 拼接成指定格式
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
function parseDateString(dateString: string): Date | null {
  // 检查输入字符串的格式是否正确
  const regex = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/;
  if (!regex.test(dateString)) {
    console.error('Invalid date format. Expected YYYY-MM-DD 00:00:00.');
    return null;
  }

  // 解析字符串中的年、月、日、时、分、秒
  const parts = dateString.split(/[- :]/);
  const year = parseInt(parts[0], 10);
  const month = parseInt(parts[1], 10) - 1; // 月份是从 0 开始的
  const day = parseInt(parts[2], 10);
  const hours = parseInt(parts[3], 10);
  const minutes = parseInt(parts[4], 10);
  const seconds = parseInt(parts[5], 10);

  // 创建 Date 对象
  const date = new Date(year, month, day, hours, minutes, seconds);

  // 验证解析后的日期是否有效
  if (
    date.getFullYear() !== year ||
    date.getMonth() !== month ||
    date.getDate() !== day ||
    date.getHours() !== hours ||
    date.getMinutes() !== minutes ||
    date.getSeconds() !== seconds
  ) {
    console.error('Invalid date.');
    return null;
  }

  return date;
}

function getDateRangeArray(start: string, end: string) {
  const startDate = new Date(start);
  const endDate = new Date(end);
  const dateArray: string[] = [];
  const currentDate = new Date(startDate);

  while (currentDate <= endDate) {
    const year = currentDate.getFullYear();
    const month = String(currentDate.getMonth() + 1).padStart(2, '0');
    const day = String(currentDate.getDate()).padStart(2, '0');
    dateArray.push(`${year}-${month}-${day}`);

    // 增加一天
    currentDate.setDate(currentDate.getDate() + 1);
  }

  return dateArray;
}
function getDateWithDaysAdded(days: number) {
  const currentDate = new Date();
  const resultDate = new Date(currentDate);
  resultDate.setDate(currentDate.getDate() + days);

  // 计算月份的最后一天
  const lastDayOfMonth = new Date(
    resultDate.getFullYear(),
    resultDate.getMonth() + 1,
    0
  ).getDate();

  // 如果加减天数后日期小于1，则调整到上个月的最后一天
  if (resultDate.getDate() < 1) {
    resultDate.setDate(1);
    resultDate.setMonth(resultDate.getMonth() - 1);
    resultDate.setDate(lastDayOfMonth);
  } else if (resultDate.getDate() > lastDayOfMonth) {
    resultDate.setDate(1);
    resultDate.setMonth(resultDate.getMonth() + 1);
  }

  const year = resultDate.getFullYear();
  const month = String(resultDate.getMonth() + 1).padStart(2, '0');
  const day = String(resultDate.getDate()).padStart(2, '0');

  return `${year}-${month}-${day}`;
}
const DateUtil = {
  now: formatCurrentDate,
  parse: parseDateString,
  /**
   * 处理下日期
   * @param date 日期
   * @param onlyDate 是否只取日期部分
   * @returns 日期字符串
   */
  resolve: (date?: string, onlyDate?: boolean) => {
    if (!date) {
      return "";
    }
    let str = date;
    if (onlyDate) {
      str = date.includes("-") ? date.substring(0, 10) : date.substring(0, 8);
    }
    if (str.startsWith("1900-01-01") || str.startsWith("19000101")) {
      str = "";
    }
    return str;
  },
  /**
   * 将日期格式在后面拼接上时分秒
   * @param date 日期
   * @returns 带时间的日期
   */
  formatToTime: (date: string) => {
    const str = date.trim();
    if (str.length === 8 || str.length === 10) {
      return `${str} 00:00:00`;
    }
    return str;
  },
  default: "1900-01-01 00:00:00",
  getDateRange: getDateRangeArray,
  getDateWithDaysAdded
}
export default DateUtil;
